
function []=PlotDiffsAll(groupsInFiles,extraInfo)

uniqueNames {1}= groupsInFiles.Peaks{1}.GroupName;

for I=1:length(groupsInFiles.Peaks)
    uniqueNames{I}=groupsInFiles.Peaks{I}.GroupName;
end

uniqueNames=unique(uniqueNames);

groupParams = cell([1 length(uniqueNames)]);
groupAverages = zeros([length(uniqueNames) length(groupsInFiles.ColNames)]);
for I=1:length(uniqueNames)
    groupIndexs =[];
    for J=1:length(groupsInFiles.Peaks)
        if (  (strcmp( groupsInFiles.Peaks{J}.GroupName, uniqueNames{I}) || strcmp( groupsInFiles.Peaks{J}.GroupName, [uniqueNames{I} '_Test'])) )
            groupIndexs = [groupIndexs J]; %#ok<AGROW>
        end
    end
    if (length(groupIndexs)>1)
        params=[];
        for KK=groupIndexs
            params = vertcat(params, groupsInFiles.Peaks{KK}.Train);
        end
        groupAverages(I,:) =  mean(params);
        groupParams{I}=params;
    end
end

groupAverages(:,1:3)=[];

distances =zeros([length(uniqueNames) length(uniqueNames)]);
for I=1:length(uniqueNames)
    for J=I+1:length(uniqueNames)
        distances(I,J)= sum(  (groupAverages(I,:)-groupAverages(J,:)).^2);
    end
end

[value idx]=max(distances);

[value2 idx2]=max(value);

furthest = [idx(idx2) idx2];

furthest(1)=2;
furthest(2)=3;

params1=groupParams{furthest(1)};
params2=groupParams{furthest(2)};

colNames = groupsInFiles.ColNames;

desired = [4:length(groupsInFiles.ColNames)];

%colNames = groupsInFiles.ColNames(desired)';


params1=params1(:,desired);
params2=params2(:,desired);

% for I=1:length(uniqueNames)
%     params1=groupParams{I};
%     amino=uniqueNames{I};
%     savedatatable(['c:\temp\hist\' amino '_All.csv'],colNames,params1);
%     
%     d1=params1(:,2);
%     [v, ia,ic]= unique(d1);
%     csP1=params1(ia,:);
%     
%     savedatatable(['c:\temp\hist\' amino '_RepeatsRemoved.csv'],colNames,csP1);
%     
% end


for I=1:size(params1,2)%-30
    
    %     if (I>=63 || I==35)
    %         d1 = params1(:,I).^.25;
    %         d2=params2(:,I).^.25;
    %     else
    %         if ( I==9)
    %             d1 = params1(:,I).^.5;
    %             d2=params2(:,I).^.5;
    %         else
    d1 = params1(:,I);
    d2 = params2(:,I);
    
    
    %     if (findstr( groupsInFiles.ColNames{I},'Maximum Peaks1'))
    %
    %            indexs=params1(:,1:3);
    %
    %            idx1=find(d1>.65);
    %            ds=d1(idx1);
    %            indexs= indexs(idx1,:);
    %
    %            idx1=find(ds<.8);
    %            ds=ds(idx1);
    %            indexs(idx1,:);
    %
    %
    %
    %     end
    
    
    %     if (findstr( groupsInFiles.ColNames{I},'Cepstrum'))
    %         d1=d1.^.25;
    %         d2=d2.^.25;
    %     end
    
    %     if (findstr( groupsInFiles.ColNames{I},'Amplitude'))
    %         d1=d1.^.5;
    %         d2=d2.^.5;
    %     end
    
    %         end
    %     end
    %
    if (I>=9)
        d1=unique(d1);
        d2=unique(d2);
    end
    
    d1(isnan(d1))=[];
    d2(isnan(d2))=[];
    
    fI=1;
    figure(fI);
    m=mean(d1);
    
    s=3*mean( abs(d1-m));
    %s=4*std(d1);
    mn=max([ min(d1) m-s 0]);
    mx=min([ max(d1) m+s]);
    
    try
        mn=min(d1);
        mx=max(d1);
        
        if (mx> m+s)
            mx=m+s;
        end
        %         if (findstr( groupsInFiles.ColNames{I},'Total Power'))
        %             mx=150;
        %         end
        if (findstr( groupsInFiles.ColNames{I},'Maximum Peaks3'))
            mx=5;
        end
        if (findstr( groupsInFiles.ColNames{I},'Maximum Peaks4'))
            mx=5;
        end
        if (findstr( groupsInFiles.ColNames{I},'Maximum Peaks1'))
            mx=5;
        end
        if (findstr( groupsInFiles.ColNames{I},'Maximum Peaks2'))
            mx=5;
        end
        %         if (findstr( groupsInFiles.ColNames{I},'ClusterInfo.Cluster FFT'))
        %             mx=mx+.005;
        %         end
        %         if (findstr( groupsInFiles.ColNames{I},'ClusterInfo.Cluster FFT30'))
        %             mx=.7e-3;
        %         end
        % %         if (findstr( groupsInFiles.ColNames{I},'ClusterInfo.Cluster FFT1'))
        % %             mx=.1;
        % %         end
        %
        %
        %         if (findstr( groupsInFiles.ColNames{I},'Average Amplitude'))
        %             mx=18;
        %         end
        %         if (findstr( groupsInFiles.ColNames{I},'Top Average'))
        %             mx=100;
        %         end
        %         if (findstr( groupsInFiles.ColNames{I},'Roughness'))
        %             mx=3000;
        %         end
        %         if (findstr( groupsInFiles.ColNames{I},'Peak-FFT-7'))
        %             mx=.03;
        %         end
        %         if (findstr( groupsInFiles.ColNames{I},'Peak-FFT-8'))
        %             mx=.02;
        %         end
        %         if (findstr( groupsInFiles.ColNames{I},'Peak-FFT-8'))
        %             mx=.01;
        %         end
        %         if (findstr( groupsInFiles.ColNames{I},'Peak-FFT-8'))
        %             mx=.01;
        %         end
        %         if (findstr( groupsInFiles.ColNames{I},'Peak-FFT-6'))
        %             mx=.03;
        %         end
        %         if (findstr( groupsInFiles.ColNames{I},'Peak-FFT-5'))
        %             mx=.04;
        %         end
        %         if (findstr( groupsInFiles.ColNames{I},'Peak Width'))
        %             mx=4;
        %         end
        %         if (findstr( groupsInFiles.ColNames{I},'iFFT-H'))
        %             mx=3e-3;
        %         end
        %         if (findstr( groupsInFiles.ColNames{I},'HighLow'))
        %             mx=.02;
        %         end
        %
        %         if (findstr( groupsInFiles.ColNames{I},'Odd'))
        %             mx=1.8;
        %              mn = .6;
        %         end
        %
        %         if (findstr( groupsInFiles.ColNames{I},'Even'))
        %             mx=3;
        %             mn = .6;
        %         end
        %         if (findstr( groupsInFiles.ColNames{I},'Odd-Even'))
        %             mx=1.2;
        %             mn = .7;
        %         end
        %
        %         if (findstr( groupsInFiles.ColNames{I},'ClusterInfo.Total Power'))
        %             mx=200;
        %         end
        %         if (findstr( groupsInFiles.ColNames{I},'Peaks In Cluster'))
        %             mx=200;
        %         end
        %
        step = s/30;
        if (findstr( groupsInFiles.ColNames{I},'Maximum'))
            step = s/35;
        end
        
        if (findstr( groupsInFiles.ColNames{I},'Cepstrum'))
            step = s/30;
        end
        
        if (findstr( groupsInFiles.ColNames{I},'Width'))
            step = s/25;
        end
        %
        %          if (strcmp( groupsInFiles.ColNames{I},'Amplitude'))
        %             mx=60;
        %          else
        %         if (findstr( 'ClusterInfo.Amplitude',groupsInFiles.ColNames{I}))
        %             mn=110;
        %             mx=300;
        %         end
        
        %     end
        
        xCenters = mn:step:mx;
        V1=hist(d1,xCenters);
        
        %         if (V1(end-1)>30)
        %             xCenters = mn:step: max(d1);
        %             V1=hist(d1,xCenters);
        %         end
        
        V2=hist(d2,xCenters);
        xCenters(end)=[];
        V1(end)=[];
        V2(end)=[];
        V1=V1/sum(V1);
        V2=V2/sum(V2);
        
        accur =round(100* sum( (max( vertcat(V1,V2)) ./ (V1+V2+.001)) .* (V1+V2) ) /sum((V1+V2)));
        
        h=plot(xCenters,V1,'k',xCenters,V2,'--k');
        set(h(1),'linewidth',1);
        set(h(2),'linewidth',2);
        set(fI, 'color', 'white');
        set(gca, 'Box', 'off');
        set(gca, 'TickDir', 'out');
        %whitebg(I);
        
        unit = 'pA';
        
        if (findstr( groupsInFiles.ColNames{I},'Freq'))
            unit = 'Normalized Amplitude';
        end
        
        if (strcmp( groupsInFiles.ColNames{I},'Frequency'))
            groupsInFiles.ColNames{I}='ClusterInfo.Frequency';
            unit = 'kHz';
        end
        
        if (strcmp( groupsInFiles.ColNames{I},'P-Freq'))
            groupsInFiles.ColNames{I}='Peak.Frequency';
            unit = 'kHz';
        end
        
        if (findstr( groupsInFiles.ColNames{I},'iFFT'))
            unit = 'Normalized Amplitude';
        end
        
        if (findstr( groupsInFiles.ColNames{I},'Width'))
            unit = 'ms';
        end
        
        if (findstr( groupsInFiles.ColNames{I},'FFT'))
            unit = 'Normalized Amplitude';
        end
        
        if (findstr( groupsInFiles.ColNames{I},'Cepstrum'))
            unit = 'Normalized Amplitude';
        end
        
        if (findstr( groupsInFiles.ColNames{I},'Maximum'))
            unit = 'kHz';
        end
        
        if (findstr( groupsInFiles.ColNames{I},'Odd'))
            unit = 'Normalized Amplitude';
        end
        
        if (findstr( groupsInFiles.ColNames{I},'Even'))
            unit = 'Normalized Amplitude';
        end
        
        if (findstr( groupsInFiles.ColNames{I},'Ratio'))
            unit = 'Normalized Amplitude';
        end
        
        
        ylabel( 'Probability Density', 'FontSize', 16, 'Rotation', 90);
        xlabel(unit, 'FontSize', 16 );
        
        hleg1 = legend([uniqueNames{furthest(1)} ': ' groupsInFiles.ColNames{desired(I)}],[ uniqueNames{furthest(2)} ': ' groupsInFiles.ColNames{desired(I)}]);
        set(hleg1,'Location','NorthEast')
        set(hleg1,'Interpreter','none')
        % title( groupsInFiles.ColNames{I});
        set(gcf,'name', groupsInFiles.ColNames{I},'numbertitle','off') ;
        
        values{1,1}=groupsInFiles.ColNames{I};
        values{1,2}='LEU';
        values{1,3}='mGly';
        for J=1:length(V1)
            values{J+1,1}=xCenters(J);
            values{J+1,2}=V1(J);
            values{J+1,3}=V2(J);
        end
        drawnow;
        name = [ num2str(accur) '_' uniqueNames{ furthest(1)} '_'  uniqueNames{furthest(2)} '_'  groupsInFiles.ColNames{I} ];
        disp(name)
        
        cell2csv(['c:\temp\hist\A_' name '.csv'] , values);
        
        filename =['c:\temp\graphs\A_' name '.png'];
        saveas(fI,filename);
        
        
        
        
    catch mex
        disp(mex)
        disp(mex.stack(1,1));
    end
end


